package thread;
//内存可见性问题
import java.util.Scanner;

public class Thread_bug {
    //添加volatile关键字保证内存可见性 禁止指令重排序
    private volatile static int flag = 0;
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            while(flag == 0){
                //循环体内为空 load被jvm优化掉了
                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
            System.out.println("t1线程结束");
        });
        Thread t2 = new Thread(()->{
            System.out.println("请输入flag的值：");
            Scanner scanner = new Scanner(System.in);
            flag = scanner.nextInt();
        });
        t1.start();
        t2.start();
    }
}
